package com.learn.sqlSession;

import com.learn.pojo.Configuration;
import com.learn.pojo.MappedStatement;

import java.beans.IntrospectionException;
import java.lang.reflect.*;
import java.sql.SQLException;
import java.util.List;

public class DefaultSqlSession implements SqlSession {

    private Configuration configuration;

    public DefaultSqlSession(Configuration configuration){
        this.configuration = configuration;
    }

    @Override
    public int execute(String statementid, Object... params) throws Exception {
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementid);
        int count = 0;
        switch (mappedStatement.getSqlType()){
            case "select":
                selectOne(statementid, params);
                break;
            case "update":
                count = update(statementid, params);
                break;
            case "insert":
                count = update(statementid, params);
                break;
            case "delete":
                count = update(statementid, params);
                break;
            default:
                throw new RuntimeException("Unknown execution method for:" + mappedStatement.getSqlType());
        }
        return count;
    }

    private int update(String statementid, Object... params) throws Exception {
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementid);
        return simpleExecutor.execute(configuration, mappedStatement, params);
    }

    @Override
    public <E> List<E> selectList(String statementid, Object... params) throws IllegalAccessException, ClassNotFoundException, IntrospectionException, InstantiationException, SQLException, InvocationTargetException, NoSuchFieldException {
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementid);
        List<Object> list = simpleExecutor.query(configuration, mappedStatement, params);
        return (List<E>) list;
    }

    @Override
    public <T> T selectOne(String statementid, Object... params) throws Exception {
        List<Object> objects = selectList(statementid,params);
        if(objects.size() == 1){
            return (T)objects.get(0);
        }else{
            throw new RuntimeException("More OR Null");
        }
    }

    @Override
    public <T> T getMapper(Class<?> mapperClass) {
        //JDK动态代理》Dao接口动态代理
        Object proxyInstance = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{mapperClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                //底层JDBC
                //准备参数1 statementid-namespace.id
                String methodName = method.getName();
                String className = method.getDeclaringClass().getName();
                String statementid = className + "." + methodName;
                System.out.println(statementid);
                //参数2
                Type genericReturnType = method.getGenericReturnType();
                //是否泛型类型参数化
                if(genericReturnType instanceof  ParameterizedType){
                    List<Object> objects = selectList(statementid, args);
                    return objects;
                }
                return execute(statementid, args);
            }
        });
        return (T) proxyInstance;
    }
}
